<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Model;
//供应商模块
class Gys extends Common
{
	   //删除
    public function drop($id){
    	try{
    		Db::table("gys")->where(["id"=>$id])->update(["is_actived"=>0]);
    		return 1;
    	}catch(\Exception $e){
        	
            return 2;
        }
    }
	//供应商列表
	public function manage($page=1,$list=10){
		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="g.is_actived=1 and g.is_deleted=0 ";

    	if($data){
    		if($data["no"])
    			$where.="and g.no='{$data["no"]}' ";
    		if($data["gys_jian"])
    			$where.="and g.gys_jian='{$data["gys_jian"]}' ";
    		if($data["type"]){
    			$gsysou=Db::table("gys_type")->where(["title"=>$data["type"]])->find();
    			if($gsysou){
    				$ck=$gsysou["id"];
    			}else{
    				$ck=0;
    			}
    			$ck='"'.$ck.'"';
    			$where.="and g.type_ids like '%{$ck}%' ";
    		}
    		
    	}else{
    		$data=["no"=>"","gys_jian"=>"","type"=>""];
    	}



    	$sum=Db::table("gys")->alias("g")->where($where)->count();
    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

          $apply=Db::table("gys")->alias("g")
        ->join("users uc","g.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","g.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where($where)->order("created_on desc")
        ->limit($start,$list)
        ->field("g.id,g.no,g.gys_name,g.gys_jian,g.sign_address,g.phone,g.contact,ec.em_name as cname,g.created_on,em.em_name as mname,g.modifiled_on,g.type_ids")
        ->select();
        $gytype=Db::table("gys_type")->select();
		for($i=0;$i<count($gytype);$i++){
			$tys[$gytype[$i]["id"]]=$gytype[$i]["title"];
		}
         for($i=0;$i<count($apply);$i++){
        	$apply[$i]["created_on"]=date("Y-m-d H:i",strtotime($apply[$i]["created_on"]));
        	if($apply[$i]["modifiled_on"]){
        		$apply[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($apply[$i]["modifiled_on"]));
        	}
        	if($apply[$i]["type_ids"]){
        		$tmp=json_decode($apply[$i]["type_ids"],true);

        		$tmps=[];
        		for($j=0;$j<count($tmp);$j++){
        			$tmps[]=$tys[$tmp[$j]];
        		}
        		$apply[$i]["type"]=implode(',',$tmps);
        	}else{
        		$apply[$i]["type"]='';
        	}
        	
        }
        $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("data",$data);
		$this->assign("apply",$apply);

		return $this->fetch();
	}
	public function details($gid){
  $gytype=Db::table("gys_type")->select();
		for($i=0;$i<count($gytype);$i++){
			$tys[$gytype[$i]["id"]]=$gytype[$i]["title"];
		}
			$gys=Db::table("gys")->alias("g")
        ->join("users uc","g.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","g.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where(["g.id"=>$gid])
        ->field("g.id,g.no,g.gys_name,g.gys_jian,g.sign_address,g.phone,g.tax_id,g.bank,g.bank_no,g.contact,ec.em_name as cname,g.created_on,em.em_name as mname,g.modifiled_on,g.type_ids,g.email,g.tel,g.fax,g.address,g.shoujian,g.shoujian_tel,g.remark,g.is_yiban")
        ->find();
        if($gys["type_ids"]){
        		$tmp=json_decode($gys["type_ids"],true);

        		$tmps=[];
        		for($j=0;$j<count($tmp);$j++){
        			$tmps[]=$tys[$tmp[$j]];
        		}
        		$gys["type_ids"]=implode(',',$tmps);
        	}else{
        		$gys["type_ids"]='';
        	}
			$this->assign("gys",$gys);
		return $this->fetch();
	}
	//新增或修改页面
	public function addgys($gid=0){
		$gytype=Db::table("gys_type")->select();
		


		$fujian=[];
		$typs=[];
		if($gid){
			$gys=Db::table("gys")->where(["id"=>$gid])->find();
			$this->assign("gys",$gys);
			if($gys["fujian"]){
				$fujian=Db::table("annex")->where("id in ({$gys["fujian"]})")->select();
			}
		
			
			if($gys["type_ids"]){
				$typs=json_decode($gys["type_ids"],true);
				for($i=0;$i<count($gytype);$i++){
					if(in_array($gytype[$i]["id"],$typs)){
						$gytype[$i]["ck"]=1;
					}else{
						$gytype[$i]["ck"]=0;
					}
				}
			}

		}
		//print_r($gytype);exit;
		$this->assign("gytype",$gytype);
			$this->assign("fujian",$fujian);	
			$this->assign("gid",$gid);
			$this->assign("typs",$typs);
		return $this->fetch();
	}

	public function addchuli(){
		$request = Request::instance();
    	$data=$request->post();
    	
    	if(!trim($data["gys_jian"]))
    		return json_encode(["msg"=>"简称不能为空","state"=>2]);
    	if(!trim($data["gys_name"]))
    		return json_encode(["msg"=>"供应商名称不能为空","state"=>2]);
    	if(!trim($data["tax_id"]))
    		return json_encode(["msg"=>"税号不能为空","state"=>2]);
    	if(!trim($data["bank"]))
    		return json_encode(["msg"=>"开户行不能为空","state"=>2]);
    	if(!trim($data["bank_no"]))
    		return json_encode(["msg"=>"银行帐号不能为空","state"=>2]);
    	if(!trim($data["phone"]))
    		return json_encode(["msg"=>"供应商电话不能为空","state"=>2]);
    	if(!trim($data["sign_address"]))
    		return json_encode(["msg"=>"注册地址不能为空","state"=>2]);
    	if(!trim($data["contact"]))
    		return json_encode(["msg"=>"联系人不能为空","state"=>2]);
    	if(!trim($data["tel"]))
    		return json_encode(["msg"=>"联系人电话不能为空","state"=>2]);
    	if(!trim($data["address"]))
    		return json_encode(["msg"=>"邮寄地址不能为空","state"=>2]);
    	if(!trim($data["shoujian"]))
    		return json_encode(["msg"=>"收件人不能为空","state"=>2]);
    	if(!trim($data["shoujian_tel"]))
    		return json_encode(["msg"=>"收件人电话不能为空","state"=>2]);
    	if(isset($data["type_ids"])){
    		$data["type_ids"]=json_encode($data["type_ids"]);
    	}else{
    		$data["type_ids"]=null;
    	}

    	if(!trim($data["no"]))
    		$data["no"]="GYS".date('Ymd').rand(0,1000).Session::get("employee_id");
    	$data["fujian"]=$data["attach"];
    	unset($data["attach"]);
    	//print_r($data);exit;
    	Db::startTrans();
    	if($data["gid"]){
    		//修改
    		$data["id"]=$data["gid"];
    		unset($data["gid"]);
    		try{
    			$data["modifiled_on"]=date("Y-m-d H:i:s");
    			$data["m_users_id"]=Session::get("login_id");
    			$res1=Db::table("gys")->update($data);
    		}catch(\Exception $e){
	        	Db::rollBack();
	            return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
	        }
	        if($res1){
				Db::commit();
	            return json_encode(["msg"=>"","state"=>1]);
	        }else{
	        	Db::rollBack();
	          return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
	        }
    	}else{
    		//增加
    		unset($data["gid"]);
    		$data["created_on"]=date("Y-m-d H:i:s");
    		$data["c_users_id"]=Session::get("login_id");
    		try{
    			$res1=Db::table("gys")->insert($data);
    		}catch(\Exception $e){
	        	Db::rollBack();
	            return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
	        }
	        if($res1){
				Db::commit();
	            return json_encode(["msg"=>"","state"=>1]);
	        }else{
	        	Db::rollBack();
	          return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
	        }
    	}
	}
	
}